package com.dianmi.seckill.controller;

import com.dianmi.seckill.common.advice.ServiceException;
import com.dianmi.seckill.entity.OrderInfo;
import com.dianmi.seckill.service.impl.OrderServiceImpl;
import com.dianmi.seckill.service.impl.SeckillServiceImpl;
import com.dianmi.seckill.vo.SeckillInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    private static final String NOT_FOUND_ORDER = "not found order";

    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private SeckillServiceImpl seckillService;

    @GetMapping(value = "query", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public OrderInfo queryOrder(@RequestBody SeckillInfo seckillInfo) {
        Long flag = seckillService.queryUserFlag(seckillInfo.getProductId(), seckillInfo.getUserId());
        if (Long.valueOf(3L).equals(flag)) {
            return orderService.queryOrder(seckillInfo.getProductId(), seckillInfo.getUserId());
        }
        throw new ServiceException(500, NOT_FOUND_ORDER);
    }
}
